package com.teama4.shop.controller;

import com.teama4.shop.annotation.NeedLogin;
import com.teama4.shop.domain.*;
import com.teama4.shop.service.CommodityCollectService;
import com.teama4.shop.service.StoreCollectService;
import com.teama4.shop.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Controller
@RequestMapping("/collect")
public class CollectController {

    @Autowired
    private CommodityCollectService commodityCollectService;

    @Autowired
    private StoreCollectService storeCollectService;

    /**
     *商品收藏 多选添加
     *
     * @param allCheckId
     */
    @NeedLogin
    @RequestMapping("/CheckedAddCommodityCollect")
    public void CheckedAddCommodityCollect(HttpServletRequest req, HttpServletResponse resp, String allCheckId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        if (allCheckId != null && allCheckId != "") {
            HttpSession session = req.getSession(false);
            if (session != null&&session.getAttribute("userId")!=null) {
                String[] allCheckIds = allCheckId.split(",");
                Integer userId = (Integer) session.getAttribute("userId");
                boolean isinsertOk = true;

                boolean isHave =false;
                List<Integer> errorList = new ArrayList<>();
                List<Integer> isHaveList = new ArrayList<>();
                for (int i = 0; i < allCheckIds.length; i++) {
                    Integer commodityId = Integer.valueOf(allCheckIds[i]);
                    CommodityCollectExample commodityCollectExample = new CommodityCollectExample();
                    CommodityCollectExample.Criteria criteria = commodityCollectExample.createCriteria();
                    criteria.andUserIdEqualTo(userId);
                    criteria.andCommodityIdEqualTo(commodityId);

                    if(commodityCollectService.selectByExample(commodityCollectExample).size()<=0){
                        CommodityCollect commodityCollect = new CommodityCollect();
                        commodityCollect.setUserId(userId);
                        commodityCollect.setCommodityId(commodityId);

                        int res = commodityCollectService.insertSelective(commodityCollect);
                        if(res<=0){
                            isinsertOk=false;
                            errorList.add(i);
                        }
                    }else {
                        isHave=true;
                        isHaveList.add(i);
                    }

                }
                //如果每个插入成功并且原来数据库没有
                if (isinsertOk&&!isHave) {
                    //如果成功
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else if(!isinsertOk) {
                    //如果插入失败
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    map.put("all",allCheckIds.length);
                    map.put("errors",errorList.size());
                    JSONUtil.printByJSON(resp, map);
                }else if(isHave){
                    //如果存在
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "exit");
                    map.put("all",allCheckIds.length);
                    map.put("haves",isHaveList.size());
                    JSONUtil.printByJSON(resp, map);
                }
            } else {
                //如果session获取不到值
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "noSession");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "isNull");
            JSONUtil.printByJSON(resp, map);
        }
    }


    /**
     *商品收藏 单个商品id添加
     * @param commodityId 商品规格id
     */
    @NeedLogin
    @RequestMapping("addByCommodityIdCollect")
    public void addByCommodityIdCollect(HttpServletResponse resp, HttpServletRequest req, Integer commodityId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null&&session.getAttribute("userId")!=null) {
            Integer userId = (Integer) session.getAttribute("userId");
            CommodityCollectExample commodityCollectExample = new CommodityCollectExample();
            CommodityCollectExample.Criteria criteria = commodityCollectExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andCommodityIdEqualTo(commodityId);
            List<CommodityCollect> commodityCollects = commodityCollectService.selectByExample(commodityCollectExample);

            if(commodityCollects.size()<=0){

                CommodityCollect commodityCollect = new CommodityCollect();
                commodityCollect.setUserId(userId);
                commodityCollect.setCommodityId(commodityId);
                int res = commodityCollectService.insertSelective(commodityCollect);
                if (res > 0) {
                    //如果成功
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果失败
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);
                }
            }else {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "exit");
                JSONUtil.printByJSON(resp, map);
            }


        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }


    }


    /**
     *商品收藏 根据商品id删除
     * @param commodityId 商品id
     */
    @NeedLogin
    @RequestMapping("delByCommodityIdCollect")
    public void delByCommodityIdCollect(HttpServletResponse resp, HttpServletRequest req, Integer commodityId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null) {
            CommodityCollectExample commodityCollectExample = new CommodityCollectExample();
            CommodityCollectExample.Criteria criteria = commodityCollectExample.createCriteria();
            criteria.andCommodityIdEqualTo(commodityId);
            int res = commodityCollectService.deleteByExample(commodityCollectExample);
            if (res > 0) {
                //如果删除成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果删除失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**
     *商品收藏 单个删除
     * @param commodityCollectId 商品收藏id
     */
    @NeedLogin
    @RequestMapping("delCommodityCollect")
    public void delCommodityCollect(HttpServletResponse resp, HttpServletRequest req, Integer commodityCollectId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null) {
            int res = commodityCollectService.deleteByPrimaryKey(commodityCollectId);
            if (res > 0) {
                //如果删除成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果删除失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**{
     * 店铺收藏 移除多个
     * @param req
     * @param resp
     * @param allCheckId
     * @throws UnsupportedEncodingException
     */
    @NeedLogin
    @RequestMapping("/delCheckedCommodityCollect")
    public void delCheckedCommodityCollect(HttpServletRequest req, HttpServletResponse resp, String allCheckId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        if (allCheckId != null && allCheckId != "") {
            String[] allCheckIds = allCheckId.split(",");

            HttpSession session = req.getSession(false);
            if (session != null) {
                CommodityCollectExample commodityCollectExample = new CommodityCollectExample();
                CommodityCollectExample.Criteria criteria = commodityCollectExample.createCriteria();
                List<Integer> allCheckIdsList = new ArrayList<>();

                for(int i=0;i<allCheckIds.length;i++){
                    allCheckIdsList.add(Integer.valueOf(allCheckIds[i]));
                }
                criteria.andIdIn(allCheckIdsList);
                int res = commodityCollectService.deleteByExample(commodityCollectExample);
                if (res > 0) {
                    //如果删除成功
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果删除失败
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);
                }
            } else {
                //如果session获取不到值
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "noSession");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "isNull");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**
     * 删除多条店铺收藏
     * @param req
     * @param resp
     * @param allCheckId
     * @throws UnsupportedEncodingException
     */
    @NeedLogin
    @RequestMapping("/delCheckedStoreCollect")
    public void delCheckedStoreCollect(HttpServletRequest req, HttpServletResponse resp, String allCheckId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        if (allCheckId != null && allCheckId != "") {
            String[] allCheckIds = allCheckId.split(",");

            HttpSession session = req.getSession(false);
            if (session != null) {
                StoreCollectExample storeCollectExample = new StoreCollectExample();
                StoreCollectExample.Criteria criteria = storeCollectExample.createCriteria();
                List<Integer> allCheckIdsList = new ArrayList<>();

                for(int i=0;i<allCheckIds.length;i++){
                    allCheckIdsList.add(Integer.valueOf(allCheckIds[i]));
                }
                criteria.andIdIn(allCheckIdsList);
                int res = storeCollectService.deleteByExample(storeCollectExample);
                if (res > 0) {
                    //如果删除成功
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果删除失败
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);
                }
            } else {
                //如果session获取不到值
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "noSession");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "isNull");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**
     * 店铺添加收藏 单个
     * @param storeId 店铺id
     */
    @NeedLogin
    @RequestMapping("addStoreCollect")
    public void addStoreCollect(HttpServletResponse resp, HttpServletRequest req, Integer storeId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null&&session.getAttribute("userId")!=null) {
            Integer userId = (Integer) session.getAttribute("userId");
            StoreCollectExample storeCollectExample = new StoreCollectExample();
            StoreCollectExample.Criteria criteria = storeCollectExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andStoreIdEqualTo(storeId);
            List<StoreCollect> storeCollects = storeCollectService.selectByExample(storeCollectExample);

            if(storeCollects.size()<=0){
                StoreCollect storeCollect = new StoreCollect();
                storeCollect.setUserId(userId);
                storeCollect.setStoreId(storeId);
                int res = storeCollectService.insertSelective(storeCollect);
                if (res > 0) {
                    //如果成功
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "ok");
                    JSONUtil.printByJSON(resp, map);
                } else {
                    //如果失败
                    HashMap<String, Object> map = new HashMap<String, Object>();
                    map.put("result", "fail");
                    JSONUtil.printByJSON(resp, map);
                }
            }else {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "exit");
                JSONUtil.printByJSON(resp, map);
            }


        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }


    }


}
